package com.hwl.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.utils.*;
import com.hwl.servicebase.dto.MemberDto;
import com.hwl.ucenter.mapper.MemberMapper;
import com.hwl.ucenter.pojo.Member;
import com.hwl.ucenter.pojo.vo.LoginVo;
import com.hwl.ucenter.pojo.vo.RegisterVo;
import com.hwl.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-10-07
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        AssertUtil.isTrue(StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code), "注册信息不得为空！");

        String realCode = redisTemplate.opsForValue().get(mobile);
        AssertUtil.isTrue(StringUtils.isEmpty(realCode), "验证码已过期，请重新申请！");
        AssertUtil.isTrue(!code.equals(realCode), "验证码不正确！请重新输入");

        //校验成功，删除验证码
        redisTemplate.delete(mobile);

        Member member = new Member()
                .setNickname(nickname).setMobile(mobile).setPassword(MD5.encrypt(password))
                .setAvatar("https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg")
                .setIsDisabled(false);

        return this.save(member);
    }

    @Override
    public RespBean login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        if (StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile) || StringUtils.isEmpty(password)) {
            return RespBean.error().message("登录信息不得为空！");
        }

        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));
        if (member == null || !MD5.encrypt(password).equals(member.getPassword())) {
            return RespBean.error().message("用户名或密码不正确！");
        }

        AssertUtil.isTrue(member.getIsDisabled(), "账号被禁用，请联系管理员!");

        JwtInfo jwtInfo = new JwtInfo();
        BeanUtils.copyProperties(member, jwtInfo);
        //生成token
        String token = JwtUtils.getJwtToken(jwtInfo, 1800);

        return RespBean.ok().message("登录成功！").data(token);
    }

    @Override
    public MemberDto getMemberDtoById(String memberId) {
        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member, memberDto);
        return memberDto;
    }

    @Override
    public Integer countRegisterNum(String date) {
        return baseMapper.selectRegisterNumByDay(date);
    }

}
